package com.liner.wzq;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class DisplayPhotoActivity extends Activity {
	
	private static final String TAG = "JoshuaTree";
	
	private ImageView mImageView;
	private String imagePath;
	private Bitmap bitmap;
	private Location lastKnownLocation;
	
	private EditText remote_place;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_display);
		
		Button addButton = (Button)findViewById(R.id.addButton);
		remote_place=(EditText)findViewById(R.id.remote_place);
		
		Intent displayIntent = getIntent();
		bitmap = (Bitmap) displayIntent.getParcelableExtra("BitmapImage");
		imagePath= displayIntent.getStringExtra("ImagePath");
		lastKnownLocation=(Location)displayIntent.getParcelableExtra("LastKnownLocation");
		
		mImageView = (ImageView)findViewById(R.id.imageview);
		mImageView.setImageBitmap(bitmap); 
		
//		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//            // Show the Up button in the action bar.
//            getActionBar().setDisplayHomeAsUpEnabled(true);
//        }
		
		addButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View view) {
				// data would be passed to cloud here?
				//finish();
				/*消息接收*/
				Handler  handler = new Handler(){
				 /* (non-Javadoc)
				         * @see android.os.Handler#handleMessage(android.os.Message)
				         */
					 @Override
					 public void handleMessage(Message msg) {
					 Bundle b=msg.getData();
					 String textStr=b.getString("textStr");
					  Log.i(TAG, "textStr is: "+textStr);
					 /*图片上传完成消息*/
					 if(textStr!=null)
					 {
						 Toast.makeText(getApplicationContext(), textStr, Toast.LENGTH_LONG).show();					 
	                 }
					 else
					 {
									 // TODO Auto-generated method stub
					 Toast.makeText(getApplicationContext(), "保存失敗", Toast.LENGTH_LONG).show();
					 }
					 super.handleMessage(msg);
					 }
				};

				FileUploadTask fileUploadTask = new FileUploadTask();
				
				String ip=remote_place.getText().toString();				
				if( ip==null || "".equals(ip))
				{
					ip="192.168.1.135";
				}
				Toast.makeText(getApplicationContext(), ip, Toast.LENGTH_LONG).show();
				        	fileUploadTask.init(bitmap,lastKnownLocation, "http://"+ip+"/xunjian/upload_file.php");
				        	fileUploadTask.setHandler(handler);
				        	fileUploadTask.execute();

				
				
			}
		});
	}
}